package com.zhiyou100.review.day015.mycollection;

import cn.hutool.core.util.RandomUtil;

import java.util.ArrayList;
import java.util.Scanner;

/**
 * @packageName: Test
 * @className: com.zhiyou100.review.day015.mycollection
 * @Description: TODO 集合测试
 * @author: yang
 * @date: 6/23/20
 */
public class Test {
    /**
     * @Description: TODO   创建一个collection 随机装10个字符串和10个整数 删除字符串 中含有a的所有元素  获取所有整数元素的平均值
     * @name: test01
     * @param: []
     * @return: void
     * @date: 6/23/20 11:41
     * @author: yang
     */
    public void test01() {
        ArrayList<Object> objects = new ArrayList<>();
        // 创建一个 ArrayList
        int max = 10;
        for (int i = 0; i < max; i++) {
            if (i % 2 == 0) {
                objects.add("a");
            }
            objects.add((char) RandomUtil.randomInt('a', 'z' + 1) + "" + (char) RandomUtil.randomInt('a', 'z' + 1));
            objects.add(RandomUtil.randomInt(1, 100 + 1));
        }
        Object[] toArray = objects.toArray();
        int cnt = 0;
        // 计数
        int sum = 0;
        // 求合
        System.out.println("更改前 ===================");
        for (Object object : objects) {
            if (object instanceof String) {
                System.out.println((String) (object));
            } else if (object instanceof Integer) {
                System.out.println((Integer) (object));
            }
        }
        for (Object object : toArray) {
            if (object instanceof String) {
                String s = (String) (object);
                if (s.contains("a")) {
                    // 包含 a 删除
                    objects.remove(object);
                }

            } else if (object instanceof Integer) {
                cnt++;
                sum += (Integer) object;
            }
        }

        System.out.println("更改后 ===================");

        for (Object object : objects) {
            if (object instanceof String) {
                System.out.println((String) (object));
            } else if (object instanceof Integer) {
                System.out.println((Integer) (object));
            }
        }

        System.out.println("平均值 " + sum / (cnt + 0.0));


    }

    /**
     * @Description: TODO 获取键盘输入一个20个字符的字符串：abaabcabc
     * *使用Arraylist  list1 装所有的字符     a  b  c  a b  c  a  b c
     * *使用Arraylist  list2 装所有但不重复的字符    a b c
     * *打印每个字符出现的次数
     * @name: test02
     * @param: []
     * @return: void
     * @date: 6/23/20 15:00
     * @author: yang
     */
    public void test02() {
        Scanner scanner = new Scanner(System.in);
        String s = scanner.nextLine();
        ArrayList<Object> list1 = new ArrayList<>();
        ArrayList<Object> list2 = new ArrayList<>();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            list1.add(c);
            // 添加
            if (!list2.contains(c)) {
                list2.add(c);
            }
        }
        System.out.println(list1);

        System.out.println(list2);
        for (Object l2 : list2) {
            if (l2 instanceof Character) {
                int cnt = 0;
                Character l21 = (Character) l2;
                // 获取要查询次数的 Character
                Object[] objects = list1.toArray();
                for (Object o : objects) {
                    if (o instanceof Character) {
                        Character o1 = (Character) o;
                        if (l21.equals(o1)) {
                            // 如果想等 ++
                            cnt++;
                        }
                    }

                }

                System.out.println(l21 + " 出现的次数是" + cnt);

            }
        }


    }

    public static void main(String[] args) {
        Test test = new Test();
        test.test02();
    }
}
